// Geogebra instruction parser interface; not a programmers interface
var GeogebraSTATE = new Object(); // record previous state of objects for high light
var GGB_HIGHLIGHT = { "Scale": 2, "Red": 255, "Green": 0, "Blue":0 }
// Enumeration of geogebra objects
var GGB_POINT = "point";
var GGB_LINE = "line";
var GGB_CIRCLE = "circle";

var GGB_VERSION = new Object();
GGB_VERSION["4"] = urlroot + "/plugin/Geogebra4";
GGB_VERSION["5"] = urlroot + "/plugin/Geogebra5";
var GGB_STABLE = "4";
var GGB_BETA = "5";

function GeogebraInit( version, params ){
}

function GeogebraParse( target, command ){
	var commands = command.split(/;/g); // commands list, each separated by ;
	var message = "";	// bug tracking message
	var ggbObject = $("#" + target).get(0);	// find the relevant geogebra object
	dmsg("Geogebra [" + target + "]");
	
	// No geogebra object, no use parsing any further
	//if ( ggbObject == null )
	//	return(message + " jQuery no Object");
	
	// process each command
	for ( var commandid = 0; commandid < commands.length; commandid ++ ){
		// parse the command into elements <command> <elements>
		var parse = commands[commandid].split(/[ {}(),]/g);
		
		if ( ggbObject == null ) {
				if ( parse[0] == "img" ) { 
					// image processing in lieu of geogebra plugin
					dmsg( "change the image in leiu of Geogebra" );
				}
			}
		else if ( parse[0] == "img" ) {
				// do nothing for alternate image
				dmsg( "ignored image selector" );
			}
		else if ( parse[0] == "color" ){ // identify color setting request
		
			// last parameter is a color reference
			var color = parse[parse.length - 1].replace(/^=/g,'');

			// colors defined using rgb encoding
			var red = 0;
			var green = 0;
			var blue = 0;
			
			// allow nicer names to lookup up values
			if ( COLOR[color] != null ) {
				red = COLOR[color].red;
				green = COLOR[color].green;
				blue = COLOR[color].blue;
//				message += COLOR[color];
			}
			
			// process each item in the list 0 - command, last is the color
			for ( var objId = 1; objId < parse.length - 1; objId ++ ){
//				message += "'" + parse[objId] + "' ";
				if (parse[objId] != "")
					// call geogebra to set the values
					ggbObject.setColor( parse[objId], red, green, blue );
			}
//			message += "set color '" + color + "'";
			
		}
		else if ( ( parse[0] == "show" ) || ( parse[0] == "hide" ) ){ // identify show and hide requests
			
			var vis = false; // set the default mode to 'hide'
			
			if (parse[0] == "show") vis = true;	// reset mode to 'show'
			
			// process each element; 0 is the command
			for ( var objId = 1; objId < parse.length; objId ++ ){
//				message += "'" + parse[objId] + "' ";
				if (parse[objId] != "")
					ggbObject.setVisible( parse[objId], vis );
			}
//			message += "vis " + vis;
			
		} 
		else if ( ( parse[0] == "play" ) || ( parse[0] == "stop" ) ){ // animate/stop objects
			
			var runIt = false; // default mode is stop
			
			if ( parse[0] == "play" ) runIt = true;	// reset mode to animate
			
			// process each element; 0 is the command
			for ( var objId = 1; objId < parse.length; objId ++ ){
				message += "<" + parse[objId] + "> ";
				if ( parse[objId] != "" )
					ggbObject.setAnimating( parse[objId], runIt );
			}

			// initiate the animation when all objects are set
			if ( runIt )
				ggbObject.startAnimation();
			else
			// any stop requests will stop all objected - EH1: track objects to decide whether to stop animations
				ggbObject.stopAnimation();
				
			message += "go " + runIt;
			
		}
		/** Added highlight function, highlight(<object list>)[=off] */
		else if ( parse[0] == "highlight" ) { // temporarily highlight an object for discussion
			// reset previous highlight objects
			for ( var ggbElem in GeogebraSTATE ) {
				//dmsg ( GeogebraSTATE[ggbElem].Name + ":" + GeogebraSTATE[ggbElem].Type + " " + GeogebraSTATE[ggbElem].Size );
				ggbObject.setVisible( ggbElem, GeogebraSTATE[ggbElem].Visible );
				ggbObject.setColor( ggbElem, GeogebraSTATE[ggbElem].Red, GeogebraSTATE[ggbElem].Green, GeogebraSTATE[ggbElem].Blue );
				if ( GeogebraSTATE[ggbElem].Type = GGB_POINT ) {
					ggbObject.setPointSize( ggbElem, GeogebraSTATE[ggbElem].Size );
				} else {
					ggbObject.setLineThickness( ggbElem, GeogebraSTATE[ggbElem].Size );
				}
				delete(GeogebraSTATE[ggbElem]); // clean up the object
			}
			//dmsg( "to do " + parse[parse.length - 1] );
			if ( parse[parse.length - 1].replace(/^=/g,'') != "off" ) { // set new state
				var objColor, objSize, objType, objVisible, objR, objB, objG;
				for ( var objId = 1; objId < parse.length - 1; objId ++ ){						
					if (parse[objId] != "") {
						// save the current state
						objColor = ggbObject.getColor(parse[objId]);
						objType = ggbObject.getObjectType(parse[objId]);
						objVisible = ggbObject.getVisible(parse[objId]);
						if ( objType == GGB_POINT ) {
							objSize = ggbObject.getPointSize(parse[objId]);
						} else {
							objSize = ggbObject.getLineThickness(parse[objId]);
						}
						objR = hexToR(objColor);
						objG = hexToG(objColor);
						objB = hexToB(objColor);
						GeogebraSTATE[parse[objId]] =
							{	"Name":		parse[objId],
								"Visible":	objVisible,
								"Red":		objR,
								"Green":	objG,
								"Blue":		objB,
								"Type":		objType,
								"Size":		objSize }; 
						//dmsg("save: " + GeogebraSTATE[parse[objId]]);
						
						ggbObject.setVisible( parse[objId], true ); // lets see it
						ggbObject.setColor( parse[objId], GGB_HIGHLIGHT.Red, GGB_HIGHLIGHT.Green, GGB_HIGHLIGHT.Blue ); // bright red
						if ( objType == GGB_POINT ) {
							ggbObject.setPointSize(parse[objId], objSize * GGB_HIGHLIGHT.Scale);
						} else {
							ggbObject.setLineThickness(parse[objId], objSize * GGB_HIGHLIGHT.Scale);
						}						
					}
				}						
			}
		}
		
		else if ( parse[0] == "label" ) {
			dmsg("process label" + parse);
			var state = parse[parse.length - 1].replace(/^=/g,'');
			var vis = false;
			vis = ( state == 'on' );
			dmsg("label state " + state + ":" + vis );
			// process each item in the list 0 - command, last is the color
			for ( var objId = 1; objId < parse.length - 1; objId ++ ){
//				message += "'" + parse[objId] + "' ";
				if (parse[objId] != "") {
					// call geogebra to set the values
					ggbObject.setLabelVisible( parse[objId], vis );
					dmsg(parse[objId] + ".visible = " + vis);
				}
			}			
		}
		
		
		else if ( parse[0] != "" ) {
			message += commands[commandid];
			// assume all other commands are Geogebra command lines
			ggbObject.evalCommand(commands[commandid]);
		}
		message += "<br />";
	}
	return (message);
}


/**
 * Library:		geogebraWrapper.js
 *
 * Description:	Encase geogebra inside a nicer (simple) interface for simple manipulation of the objects
 *
 * Content:
 *
 * ToDo:
 *  GGB1	enable use of images to replace geogebra applet for relevant devices
 *	GGB2	enhance the GGB to manipulate SVG images instead
 *	GGB3	creation tag built into 
 *
 * History:
 *  Version		Date		Author		Description
 *	0.01		2011-12-20	PBoothroyd	Library begun to allow automatic creation of standard elements for webpages.
 *			EH1: track list of objects that are animated to turn them off.
 */
 